import path from "path"
import { defineConfig } from "rollup"
import json from "@rollup/plugin-json"
import resolvePlugin from "@rollup/plugin-node-resolve"
import ts from "rollup-plugin-typescript2"

const packagesDir = path.resolve(__dirname, "packages")
const packageDir = path.resolve(packagesDir, process.env.TARGET)
const resolve = p => path.resolve(packageDir, p)
const pkg = require(resolve("package.json"))
const name = path.basename(packageDir)

const outputConfig = {
  "esm-bundler": {
    file: resolve(`dist/${name}-esm-bundler.js`),
    format: "esm"
  },
  cjs: {
    file: resolve(`dist/${name}-cjs.js`),
    format: "commonjs"
  },
  global: {
    file: resolve(`dist/${name}-global.js`),
    format: "iife"
  }
}
// package.json里面的自定义属性
const options = pkg.buildOptions

function createConfig(format, output) {
  output.name = options.name
  output.sourcemap = true

  return defineConfig({
    input: resolve("src/index.ts"),
    output,
    plugins: [json(), ts({ tsconfig: path.resolve(__dirname, "tsconfig.json") }), resolvePlugin()]
  })
}

export default options.format.map(format => createConfig(format, outputConfig[format]))
